<template>
  <div>
    <div class="goods">
      <div class="menu-wrapper" ref="menuWrapper">
        <ul>
          <li v-for="(item, index) in goods" :key="index" class="menu-item" :class="{'current': currentIndex===index}" @click="selectMenu(index, $event)">
            <span class="text border-1px">
              <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>{{item.name}}
            </span>
          </li>
        </ul>
      </div>
      <div class="goods-wrapper" ref="goodsWrapper">
        <ul>
          <li v-for="(item, index) in goods" :key="index" class="goods-list">
            <h1 class="title">{{item.name}}</h1>
            <ul>
              <li v-for="(food, key) in item.foods" :key="key" class="goods-item border-1px" @click="selectFood(food)">
                <div class="img">
                  <img :src="food.icon" width="57" height="57">
                </div>
                <div class="content">
                  <h2 class="name">{{food.name}}</h2>
                  <p class="desc">{{food.description}}</p>
                  <div class="extra">
                    <span class="count">月售{{food.sellCount}}份</span>
                    <span>好评率{{food.rating}}%</span>
                  </div>
                  <div class="price">
                    <span class="now-price">¥{{food.price}}</span>
                    <span v-show="food.oldPrice" class="old-price">¥{{food.oldPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                    <cartcontrol :food="food"></cartcontrol>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <shopcart :selectGoods="selectGoods" :deliveryPrice="seller.deliveryPrice" 
        :minPrice="seller.minPrice"></shopcart>
    </div>
    <food :food="selectedFood" ref="food"></food>
  </div>
</template>

<script>
import axios from 'axios';
import BScroll from 'better-scroll';
import shopcart from 'components/shopcart/shopcart';
import cartcontrol from 'components/cartcontrol/cartcontrol';
import food from 'components/food/food';

const ERR_OK = 0;
export default {
  props: {
    seller: {
      type: Object
    }
  },
  data () {
    return {
      goods: [],
      listHeight: [],
      scrollY: 0,
      selectedFood: {},
      foodFlag: false
    };
  },
  computed: {
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i;
        }
      };
      return 0;
    },
    selectGoods () {
      let select = [];
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            select.push(food);
          }
        });
      });
      return select;
    }
  },
  created () {
    const _this = this;
    _this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
    axios.get('/api/goods')
      .then(function (response) {
        if (response.data.status === ERR_OK) {
          _this.goods = response.data.data;
          _this.$nextTick(() => {
            _this._initScroll();
            _this._calculateHeight();
          });
        }
      });
  },
  methods: {
    _initScroll () {
      this.menuScroll = new BScroll(this.$refs.menuWrapper, {
        click: true
      });
      this.goodsScroll = new BScroll(this.$refs.goodsWrapper, {
        probeType: 3,
        click: true
      });
      this.goodsScroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    _calculateHeight () {
      let height = 0;
      let goodList = this.$refs.goodsWrapper.getElementsByClassName('goods-list');
      this.listHeight.push(height);
      for (let i = 0; i < goodList.length; i++) {
        let item = goodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
    },
    selectMenu (index, event) {
      if (!event._constructed) {
        return;
      }
      let goodList = this.$refs.goodsWrapper.getElementsByClassName('goods-list');
      let ele = goodList[index];
      this.goodsScroll.scrollToElement(ele, 300);
    },
    selectFood (food) {
      this.selectedFood = food;
      this.$refs.food.show();
    }
  },
  components: {
    shopcart,
    cartcontrol,
    food
  }
};
</script>

<style lang="stylus" scoped="scoped">
@import '../../common/stylus/mixin'
@import '../../common/stylus/base'
  .goods
    display: flex
    position: absolute
    top: 179px
    bottom: 48px
    width: 100%
    overflow: hidden
    .menu-wrapper
      flex: 0 0 80px
      width: 80px
      background-color: #f3f5f7
      .menu-item
        display: table
        padding: 0 12px
        width: 56px
        height: 54px
        line-height: 14px
        &.current
          position: relative
          margin-top: -1px
          // z-index: 1
          background-color: #fff
          font-weight: 700
          .text
            border-none()
        .text
          display: table-cell
          width: 56px
          font-size: 12px
          vertical-align: middle
          border-bottom-1px(rgba(7, 17, 27, .1))
          .icon
            display: inline-block
            width: 12px
            height: 12px
            margin-right: 2px
            vertical-align: top
            background-size: 12px 12px
            background-repeat: no-repeat
            &.decrease
              bg-image('decrease_3')
            &.discount
              bg-image('discount_3')
            &.guarantee
              bg-image('guarantee_3')
            &.invoice
              bg-image('invoice_3')
            &.special
              bg-image('special_3')
    .goods-wrapper
      flex: 1
      .title
        height: 26px
        line-height: 26px
        padding-left: 14px 
        border-left: 2px solid  #d9dde1
        font-size: 12px
        color: rgb(147, 153, 159)
        background-color: #f3f5f7
      .goods-item
        display: flex
        margin: 18px
        padding-bottom: 18px
        border-bottom-1px(rgba(7, 17, 27, .1))
        &:last-child
          border-none()
        .img
          flex: 0 0 57px
          margin-right: 10px
        .content
          flex: 1
          .name
            margin: 2px 0 8px 0
            font-size: 14px
            line-height: 14px
            color: rgb(7, 17, 27)
          .desc,.extra
            margin: 8px 0
            font-size: 10px
            line-height: 12px
            color: rgb(147, 153, 159)
          .extra
            margin-bottom: 0
            .count
              margin-right: 12px
          .price
            height: 24px
            line-height: 24px
            font-weight: 700
            .now-price
              display: inline-block
              margin-right: 8px
              font-size: 14px
              color: rgb(240, 20, 20)
              vertical-align: top
            .old-price
              display: inline-block
              text-decoration: line-through
              font-size: 10px
              color: rgb(147, 153, 159)
              vertical-align: top
          .cartcontrol-wrapper
            position: absolute
            right: 0
            bottom: 12px
</style>
